﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace Proggy.Data.Entities
{
    /// <summary>
    /// PublishableNode
    /// </summary>
    public class PublishableNode : NodeBase
    {
        /// <summary>
        /// Constructor - sets children to not be null
        /// </summary>
        public PublishableNode()
        {
            this.ChildNodes = new HashSet<PublishableNode>();
            this.AllowedViewerGroups = new HashSet<Group>();
            this.AllowedEditorGroups = new HashSet<Group>();
        }

        /// <summary>
        /// The unique node id
        /// </summary>
        [Key]
        public int PublishableNodeId { get; set; }

        /// <summary>
        /// Draft node id or null
        /// </summary>
        //public int? DraftNodeId { get; set; }

        /// <summary>
        /// The draft version or null
        /// </summary>
        //[ForeignKey("DraftNodeId")]
        public virtual DraftNode DraftNode { get; set; }       

        /// <summary>
        /// Parent node id
        /// </summary>
        //public int? ParentNodeId { get; set; }

        /// <summary>
        /// The parent node (except for root node)
        /// </summary>
        public virtual PublishableNode ParentNode { get; set; }

        /// <summary>
        /// The child nodes for this node
        /// </summary>
        public virtual ICollection<PublishableNode> ChildNodes { get; set; }

        /// <summary>
        /// Zero-indexed order of node in parent container
        /// </summary>
        public int OrderInParent { get; set; }

        /// <summary>
        /// Zero-indexed level in tree (ie 0 = homepage)
        /// </summary>
        public int LevelInTree { get; set; }

        /// <summary>
        /// Last published
        /// </summary>
        public DateTime? FirstPublished { get; set; }

        /// <summary>
        /// If true, this is a published node instance
        /// </summary>
        public bool IsPublished
        {
            get
            {
                return this.FirstPublished != null;
            }
        }

        /// <summary>
        /// True if there is a draft version available
        /// </summary>
        public bool HasDraft
        {
            get
            {
                return this.DraftNode != null;
            }
        }

        /// <summary>
        /// Returns true if parent node is not null
        /// </summary>
        public bool HasParent
        {
            get
            {
                return this.ParentNode != null;
            }
        }

        /// <summary>
        /// Returns true if child nodes count > 0
        /// </summary>
        public bool HasChildren
        {
            get
            {
                return this.ChildNodes.Count > 0;
            }
        }

        /// <summary>
        /// Is root node
        /// </summary>
        public bool IsRoot
        {
            get
            {
                return (!this.HasParent);// && (this.LevelInTree == 0);
            }
        }

        /// <summary>
        /// Last published (if published yet)
        /// </summary>
        public DateTime? LastPublished { get; set; }

        /// <summary>
        /// Last published (if published yet)
        /// </summary>
        public string LastPublishedUsername { get; set; }

        public virtual ICollection<Group> AllowedViewerGroups { get; set; }
        public virtual ICollection<Group> AllowedEditorGroups { get; set; }
    }
}
